import sys, pygame

from scrape.render import RenderManager

pygame.init()

FPS = 60

WIDTH, HEIGHT = 640, 480
screen_width, screen_height = 640, 480

TITLE = "Flow-Based Visual Scripting System"

RADIUS = 5

def clean_up_and_exit():
    print('Stopping...')
    pygame.quit()
    sys.exit()

manager: RenderManager = RenderManager()
manager.new_node(20, 20)

def pygame_loop():
    global screen
    global manager
    global fps_clock
    global screen_width
    global screen_height

    fps_clock = pygame.time.Clock()
    screen = pygame.display.set_mode((screen_width, screen_height), pygame.RESIZABLE)

    pygame.display.set_caption(TITLE)

    while True:
        screen.fill((255, 255, 255))
        mouse_pos = pygame.mouse.get_pos()
        manager.set_cursor_pos(*mouse_pos)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                clean_up_and_exit()
            elif event.type == pygame.KEYDOWN:
                if event.key in [pygame.K_q, pygame.K_ESCAPE]:
                    clean_up_and_exit()
            elif event.type == pygame.VIDEORESIZE:
                screen_width = event.w
                screen_height = event.h

        manager.draw(screen)

        pygame.display.flip()
        fps_clock.tick(FPS)

pygame_loop()
